package guda.house.web.action.sys.village.building.unit;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.UnitBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.form.village.UnitEditForm;
import guda.house.biz.form.village.UnitForm;
import guda.house.biz.query.UnitQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dataobject.BuildingDO;
import guda.house.dal.dataobject.UnitDO;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by well on 15/11/13.
 */
@Controller
public class UnitAction {

    @Autowired
    private UnitBiz unitBiz;

    @RequestMapping(value = "sys/village/building/unit/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        long buildingId = RequestUtil.getInt(request, "buildingId");
        if(buildingId == 0){
            return "redirect:/error.htm?code="+ CommonResultCode.PARAM_MISS;
        }
        UnitQuery unitQuery = new UnitQuery();
        unitQuery.setBuildingId(buildingId);
        unitQuery.setPageNo(pageId);
        unitQuery.setPageSize(pageSize);
        modelMap.put("query",unitQuery);
        BizResult bizResult = unitBiz.list(unitQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            BuildingDO buildingDO = (BuildingDO)bizResult.data.get("buildingDO");
            unitQuery.setVillageId(buildingDO.getVillageId());
            return "sys/village/building/unit/list.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }
    }

    @RequestMapping(value = "sys/village/building/unit/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, UnitEditForm unitEditForm,
                       BindingResult result, Map<String,Object> model) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = unitBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            UnitDO unitDO =  (UnitDO) bizResult.data.get("unitDO");
            unitEditForm.initForm(unitDO);
            bizResult = unitBiz.queryForInit(unitDO.getBuildingId());
            BuildingDO buildingDO = (BuildingDO)bizResult.data.get("buildingDO");
            unitEditForm.setVillageId(buildingDO.getVillageId());
            return "sys/village/building/unit/edit.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/village/building/unit/detail.htm", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = unitBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/village/building/unit/detail.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/village/building/unit/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, UnitForm unitForm,
                         BindingResult result) {
        long buildingId = RequestUtil.getInt(request, "buildingId");
        if(buildingId == 0){
            return "redirect:/error.htm?code="+ CommonResultCode.PARAM_MISS;
        }
        BizResult bizResult = unitBiz.queryForInit(buildingId);
        BuildingDO buildingDO = (BuildingDO)bizResult.data.get("buildingDO");
        unitForm.setVillageId(buildingDO.getVillageId());
        unitForm.setBuildingId(buildingId);
        return "sys/village/building/unit/create.vm";
    }

    @RequestMapping(value = "sys/village/building/unit/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap,@Valid UnitForm unitForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            return "sys/village/building/unit/create.vm";
        }
        UnitDO unitDO = unitForm.toDO();
        BizResult bizResult = unitBiz.create(unitDO);
        if (bizResult.success) {
            return "redirect:/sys/village/building/unit/list.htm?buildingId="+unitDO.getBuildingId();
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/village/building/unit/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap,@Valid UnitEditForm unitEditForm,
                           BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {

            return "sys/village/building/unit/edit.vm";
        }
        UnitDO unitDO = unitEditForm.toDO();
        BizResult bizResult = unitBiz.update(unitDO);
        if (bizResult.success) {
            return "redirect:/sys/village/building/unit/list.htm?buildingId="+unitDO.getBuildingId();
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/village/building/unit/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = unitBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }
}
